<?php

//------------------------
// 商品分类控制器
//-------------------------
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\GoodsCate as GoodsCateModel;
use app\common\model\GoodsBrand as GoodsBrandModel;
use app\common\model\GoodsCateBrand as GoodsCateBrandModel;
use app\common\model\GoodsType as GoodsTypeModel;
use think\Db;

class GoodsCate extends AdminBase
{
    protected $GoodsCateModel;
    protected $GoodsBrandModel;
    protected $GoodsCateBrandModel;
    protected $GoodsTypeModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->GoodsCateModel        = new GoodsCateModel();
        $this->GoodsBrandModel       = new GoodsBrandModel();
        $this->GoodsCateBrandModel   = new GoodsCateBrandModel();
        $this->GoodsTypeModel        = new GoodsTypeModel();
    }


    public function index(){
        $info = $this->GoodsCateModel->getTree();

        $this->assign('info',$info);

        return $this->fetch('index');
    }


    // 添加页面
    public function add(){
        $cateInfo = $this->GoodsCateModel->getTree();
        $this->assign('cateInfo',$cateInfo);

        $brandInfo = $this->GoodsBrandModel->select()->toArray();
        $this->assign('brandInfo',$brandInfo);

        $typeInfo = $this->GoodsTypeModel->select()->toArray();
        $this->assign('typeInfo',$typeInfo);

    	return $this->fetch('add');
    }

    // 编辑页面
    public function edit(){
        $id = $this->request->param('id',0,'intval');

        $cateInfo = $this->GoodsCateModel->getTree();
        $cateChildrenInfo = $this->GoodsCateModel->getChildren($id);
        $this->assign('cateInfo',$cateInfo);
        $this->assign('cateChildrenInfo',$cateChildrenInfo);

        $brandInfo = $this->GoodsBrandModel->select()->toArray();
        $this->assign('brandInfo',$brandInfo);

        $cateBrandInfo = $this->GoodsCateBrandModel->where('cate_id',$id)->field('brand_id')->select()->toArray();
        $cateBrandInfo = two_to_one($cateBrandInfo,'brand_id');
        $this->assign('cateBrandInfo',$cateBrandInfo);

        $typeInfo = $this->GoodsTypeModel->select()->toArray();
        $this->assign('typeInfo',$typeInfo);

        $info = $this->GoodsCateModel->where("id",$id)->find()->toArray();
        // 初始化检索属性集合
        $searchAttrData = array();

        if ($info['attr_key']) {
            $searchAttrData = Db('TypeAttr')->field('id,attr_name,type_id')->where('id','in',$info['attr_key'])->select()->toArray();
        }

        $this->assign('searchAttrData',$searchAttrData);

        $this->assign('info',$info);
        return $this->fetch('edit');
    }

    /**
     * @desc 添加、编辑操作
     */
    public function addProc() {
        $data = $this->request->param();
        $id   = $this->request->param('id',0,'intval');

        $validate_result = $this->validate($data, 'GoodsCate');
        if ($validate_result !== true) {
            $this->error($validate_result);
        }

        // 处理分类检索属性
        $attr_key = $data['attr_id'];

        foreach ($attr_key as $k => $v) {
            if (empty($v)) {
                unset($attr_key[$k]);
            }
        }

        if($attr_key){
            $attr_key = array_unique($attr_key);
            $data['attr_key'] = implode(',', $attr_key);
        } else {
            $data['attr_key'] = '';
        }

        if ($id) {
            // 编辑
            $res = $this->GoodsCateModel->allowField(true)->save($data,$id);
            $act = "编辑";
            $cate_id = $id;
        } else {
            // 添加
            $res = $this->GoodsCateModel->allowField(true)->save($data);
            $act = "添加";
            $cate_id = $this->GoodsCateModel->id;
        }

        if ($res === false) {
            $this->error($this->GoodsCateModel->getError());
        } else {
            // 如果添加成功，且设置了分类品牌
            if (isset($data['brand_id'])) {
                $this->GoodsCateBrandModel->setBrand($cate_id,$data['brand_id']);
            }
            $this->success('分类'.$act.'成功');
        }
    }



    // 删除操作
    public function delete(){
        $id = $this->request->param('id',0,'intval');

        if (!$id) {
            $this->error('请选择需要删除的数据');
        }

        $has = $this->GoodsCateModel->where('parent_id',$id)->find();

        if ($has) {
            $this->error('当前分类下面还有子分类');
        }

        $res = $this->GoodsCateModel->where('id',$id)->delete();

        if ($res == false) {
            $this->error($this->GoodsCateModel->getError());
        } else{
            $this->GoodsCateBrandModel->where('cate_id',$id)->delete();
            $this->success('删除成功');
        }
    }



}